package com.mftour.moudles.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mftour.common.Constant;
import com.mftour.moudles.entity.User;
import com.mftour.moudles.service.UserService;
import com.mftour.moudles.vo.AjaxResponse;
import com.mftour.moudles.vo.ShowPage;
import com.mftour.moudles.vo.search.SearchUserPagingVo;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * This class was generated by Bill Generator.
 * This class corresponds to the database table zdm_console..users  用户
 *
 * @zdm generated 2020-11-02 10:01
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {
    @Autowired
    UserService userSer;

    @RequestMapping(value = "/ajax/list", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据条件获取用户列表", notes = "返回用户列表")
    public ShowPage getUserList(SearchUserPagingVo searchUserPagingVo) {
        List<User> userList = userSer.selectListSelectivePaging(searchUserPagingVo);
        ShowPage page = new ShowPage(searchUserPagingVo, userList);
        return page;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "根据条件获取用户列表", notes = "返回用户列表")
    public ModelAndView toUserList(SearchUserPagingVo searchUserPagingVo) {
        ModelAndView mv = new ModelAndView("user/userList");
        List<User> userList = userSer.selectListSelectivePaging(searchUserPagingVo);
        ShowPage page = new ShowPage(searchUserPagingVo, userList);
        mv.addObject("page", page);
        mv.addObject("searchUserPagingVo", searchUserPagingVo);
        return mv;
    }

    @RequestMapping(value = "/ajax/save", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增用户", notes = "新增用户")
    @ApiImplicitParams(value = { @ApiImplicitParam(required=false,name="userId",paramType="query") })
    public AjaxResponse<User> save(User user) {
        user.setUserId(null);
        user.setCreatedUser(loginUser.getUserId().toString());
        AjaxResponse<User> ajaxResponse = new AjaxResponse<User>(Constant.RS_CODE_ERROR, "添加用户出错！");
        request.setAttribute("ajaxResponse", ajaxResponse);
        userSer.insertSelective(user);
        ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
        ajaxResponse.setMsg("添加用户成功！");
        return ajaxResponse;
    }

    @RequestMapping(value = "/ajax/del", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据pk删除用户", notes = "根据删除用户")
    @ApiImplicitParams(value = { @ApiImplicitParam(required=true,name="userId",value="pk",paramType="query") })
    public AjaxResponse<Object> del(Integer userId) {
        AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "pk不能为null！");
        if(userId==null){
            return ajaxResponse;
        }
        ajaxResponse.setMsg("删除用户失败！");
        request.setAttribute("ajaxResponse", ajaxResponse);
        userSer.deleteByPrimaryKey(userId);
        ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
        ajaxResponse.setMsg("删除用户成功！");
        return ajaxResponse;
    }

    @RequestMapping(value = "/ajax/delAll", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据pk集合删除用户", notes = "根据pk集合删除用户，以','为分隔符")
    @ApiImplicitParams(value = { @ApiImplicitParam(required=true,name="userIds",value="pk集合",paramType="query",allowMultiple=true,dataType="Integer") })
    public AjaxResponse<Object> delAll(Integer[] userIds) {
        AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "请选择删除项！");
        if(userIds == null||userIds.length < 0) {
            return ajaxResponse;
        }
        ajaxResponse.setMsg("删除用户失败!");
        request.setAttribute("ajaxResponse", ajaxResponse);
        userSer.deleteByPKs(userIds);
        ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
        ajaxResponse.setMsg("删除用户成功!");
        return ajaxResponse;
    }

    @RequestMapping(value = "/ajax/update", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "根据pk更新用户", notes = "根据pk更新用户，属性为null的不更新")
    public AjaxResponse<Object> update(User user) {
        if(user.getUserId()==null){
            return new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "请设置pk！");
        }
        AjaxResponse<Object> ajaxResponse = new AjaxResponse<Object>(Constant.RS_CODE_ERROR, "修改用户出错！");
        request.setAttribute("ajaxResponse", ajaxResponse);
        userSer.updateByPrimaryKeySelective(user);
        ajaxResponse.setCode(Constant.RS_CODE_SUCCESS);
        ajaxResponse.setMsg("修改用户成功！");
        return ajaxResponse;
    }

    @RequestMapping(value = "/ajax/entity", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据pk获取用户", notes = "根据pk获取用户")
    public AjaxResponse<User> getEntity(Integer userId) {
        if(userId==null){
            return new AjaxResponse<User>(Constant.RS_CODE_ERROR, "请选择pk！");
        }
        User user = userSer.selectByPrimaryKey(userId);
        AjaxResponse<User> ajaxResponse = new AjaxResponse<User>(Constant.RS_CODE_SUCCESS, "获取用户成功！");
        if (user != null) {
            ajaxResponse.setData(user);
        } else {
            ajaxResponse.setCode(Constant.RS_CODE_ERROR);
            ajaxResponse.setMsg("获取用户失败！");
        }
        return ajaxResponse;
    }
}